﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;

namespace Web_cuiki
{
    public partial class AddProduct : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadPage();
            }
            for (int i = 1900; i < DateTime.Now.Year; i++)
            {
                ddlNam.Items.Add(i.ToString());
            }
        }
        public void LoadPage()
        {
            List<KhuyenMaiDTO> dsKhuyenMai = KhuyenMaiDTO.ListConvert(KhuyenMaiDAO.laydsKieuKhuyenMai());
            List<TacGiaDTO> dsTacGia = TacGiaDTO.ListConvert(TacGiaDAO.laydsTacGia());
            List<LinhVucDTO> dsLinhVuc = LinhVucDTO.ConvertList(TheLoaiDAO.LayDSTheLoai());
            List<LoaiTaiLieuDTO> dsLoaiTaiLieu = LoaiTaiLieuDTO.ConvertList(KieuSPDAO.laydsKieuSanPham());
            this.LoadListKhuyenMai(dsKhuyenMai);
        
            this.LoadListTacGia(dsTacGia);
          
            this.LoadListLinhVuc(dsLinhVuc);
         
            this.LoadListLoaiSP(dsLoaiTaiLieu);
        }
        protected void btn_Them_Click(object sender, EventArgs e)
        {
            string tenTaiLieu = txt_Ten.Text;
            string maKieu = list_LoaiSP.SelectedValue;
            string maTheLoai = list_TheLoai.SelectedValue;
            string maTacGia = list_TacGia.SelectedValue;
            string moTa = txt_Mota.Text;
            string ngay = list_Ngay.SelectedValue;
            string thang = list_Thang.SelectedValue;
            string nam = ddlNam.Text;
            string soLuong = this.txt_SoLuong.Text;
            string gia = txt_Gia.Text;
            string maKhuyenMai = list_KhuyenMai.SelectedValue;
            string hAnh = txt_HinhAnh.Text;
            int kq = 0;
            try
            {
                DateTime date = new DateTime(Convert.ToInt32(nam), Convert.ToInt32(thang), Convert.ToInt32(ngay));

                 kq = SanPhamDAO.ThemSanPham(tenTaiLieu, Convert.ToInt32(maKieu), Convert.ToInt32(maTheLoai), Convert.ToInt32(maTacGia),
                    moTa, date, Convert.ToInt32(soLuong), Convert.ToDouble(gia), Convert.ToInt32(maKhuyenMai),hAnh);
            }
            catch (FormatException ex)
            {
                
            }
            if (kq == 1)
            {
                this.thongBao.Text = "Thêm sản phẩm thành công!!";
                this.txt_Gia.Text = "";
                this.txt_Mota.Text = "";
                this.txt_SoLuong.Text = "";
                this.txt_Ten.Text = "";
                this.ddlNam.SelectedIndex = 0;
                this.list_LoaiSP.SelectedIndex = 0;
                this.list_KhuyenMai.SelectedIndex = 0;
                this.list_Ngay.SelectedIndex = 0;                
                this.list_Thang.SelectedIndex = 0;                
            }
            else
                thongBao.Text = "Thêm sản phẩm thất bại!!!";
        }
        public void LoadListKhuyenMai( List<KhuyenMaiDTO> dsKhuyenMai)
        {
            DataTable dt = new DataTable();

            // Define the columns of the table.
            dt.Columns.Add(new DataColumn("ColorTextField", typeof(String)));
            dt.Columns.Add(new DataColumn("ColorValueField", typeof(String)));

            for (int i = 0; i < dsKhuyenMai.Count< KhuyenMaiDTO>(); i++)
            {
                dt.Rows.Add(CreateRow(dsKhuyenMai[i].PhanTram.ToString(), dsKhuyenMai[i].MaKhuyenMai.ToString(), dt));
            }
            DataView dv = new DataView(dt);
            this.list_KhuyenMai.DataSource = dv;
            list_KhuyenMai.DataTextField = "ColorTextField";
            list_KhuyenMai.DataValueField = "ColorValueField";
            list_KhuyenMai.DataBind();

        }
        public void LoadListTacGia( List<TacGiaDTO> dsTacGia)
        {
            DataTable dt = new DataTable();

            // Define the columns of the table.
            dt.Columns.Add(new DataColumn("ColorTextField", typeof(String)));
            dt.Columns.Add(new DataColumn("ColorValueField", typeof(String)));

            for (int i = 0; i < dsTacGia.Count< TacGiaDTO>(); i++)
            {
                dt.Rows.Add(CreateRow(dsTacGia[i].TenTacGia, dsTacGia[i].MaTacGia.ToString(), dt));
            }
            DataView dv = new DataView(dt);
            this.list_TacGia.DataSource = dv;
            list_TacGia.DataTextField = "ColorTextField";
            list_TacGia.DataValueField = "ColorValueField";
            list_TacGia.DataBind();
        }
        public void LoadListLinhVuc( List<LinhVucDTO> dsTacGia)
        {
            DataTable dt = new DataTable();

            // Define the columns of the table.
            dt.Columns.Add(new DataColumn("ColorTextField", typeof(String)));
            dt.Columns.Add(new DataColumn("ColorValueField", typeof(String)));

            for (int i = 0; i < dsTacGia.Count< LinhVucDTO>(); i++)
            {
                dt.Rows.Add(CreateRow(dsTacGia[i].TenLinhVuc, dsTacGia[i].MaLinhVuc.ToString(), dt));
            }
            DataView dv = new DataView(dt);
            this.list_TheLoai.DataSource = dv;
            list_TheLoai.DataTextField = "ColorTextField";
            list_TheLoai.DataValueField = "ColorValueField";
            list_TheLoai.DataBind();
        }
        public void LoadListLoaiSP( List<LoaiTaiLieuDTO> dsTacGia)
        {
            DataTable dt = new DataTable();

            // Define the columns of the table.
            dt.Columns.Add(new DataColumn("ColorTextField", typeof(String)));
            dt.Columns.Add(new DataColumn("ColorValueField", typeof(String)));

            for (int i = 0; i < dsTacGia.Count< LoaiTaiLieuDTO>(); i++)
            {
                dt.Rows.Add(CreateRow(dsTacGia[i].TenLoai, dsTacGia[i].MaLoaiTaiLieu.ToString(), dt));
            }
            DataView dv = new DataView(dt);
            this.list_LoaiSP.DataSource = dv;
            list_LoaiSP.DataTextField = "ColorTextField";
            list_LoaiSP.DataValueField = "ColorValueField";
            list_LoaiSP.DataBind();
        }


        public DataRow CreateRow(String Text, String Value, DataTable dt)
        {

            DataRow dr = dt.NewRow();

            dr[0] = Text;
            dr[1] = Value;

            return dr;

        }

    }
}